package com.example.shop_2304.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseFragment
import com.example.shop_2304.R
import com.example.shop_2304.adapter.GoodMutilAdapter
import com.example.shop_2304.adapter.GoodsAdapter
import com.example.shop_2304.adapter.OneAdapter
import com.example.shop_2304.adapter.TwoAdapter
import com.example.shop_2304.databinding.FragmentCategoryBinding
import com.example.shop_2304.room.AppDbUtils
import com.example.shop_2304.room.HistoryEntity
import com.example.shop_2304.search.SearchViewModel
import com.google.android.material.chip.Chip
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener


class CategoryFragment : BaseFragment<FragmentCategoryBinding,CategoryViewModel>() {
    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
    lateinit var goodsAdapter: GoodsAdapter

    var cid = 0//选中的分类id
    var page = 1//当前页面

    override fun initView() {
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)

        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        binding.rvTwo.layoutManager = GridLayoutManager(context,4)

        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = LinearLayoutManager(context)
        //请求一二级分类
        viewModel.category()

        //点击一级分类  一级分类文字发生变化  二级数据 发生变化   三级数据发生变化
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            oneAdapter.setType(oneAdapter.data[position].id)
            //二级数据变化

            twoAdapter.data.clear()
            twoAdapter.data.addAll(oneAdapter.data[position].child)
            twoAdapter.notifyDataSetChanged()
            //三级数据
            cid = twoAdapter.data[0].id
            page = 1
            twoAdapter.setType(cid)
            viewModel.goods(cid,"",5,false,page,"","")
        }

        //点击二级分类  二级分类文字发生变化  三级数据发生变化
        twoAdapter.setOnItemClickListener { adapter, view, position ->

            cid = twoAdapter.data[position].id
            page = 1
            twoAdapter.setType(cid)
            viewModel.goods(cid,"",5,false,page,"","")
        }

        binding.smart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.goods(cid,"",5,false,page,"","")
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.goods(cid,"",5,false,page,"","")
                binding.smart.finishLoadMore()
            }
        })

        //商品跳详情
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/XqActivity").withInt("gid",goodsAdapter.data[position].id).navigation()
        }
    }



    override fun initData() {
        viewModel.categorySuccess.observe(this){
            if(it.code==200){
                //一级数据添加
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //二级数据添加 ---注意显示的是一级数据第一个的child
                twoAdapter.data.clear()
                twoAdapter.data.addAll(oneAdapter.data[0].child)
                twoAdapter.notifyDataSetChanged()
                //加载三级分类数据  显示的是 二级分类的第一个
                cid = twoAdapter.data[0].id
                viewModel.goods(cid,"",5,false,page,"","")
            }
        }
        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }

    }

    override fun getLayout(): Int = R.layout.fragment_category

    override fun getViewModel(): Class<CategoryViewModel> = CategoryViewModel::class.java

}